package com.eedi.framework.notice.dal.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.notice.controller.org.vo.OrgNoticeReadReq;
import com.eedi.framework.notice.controller.org.vo.OrgNoticeReadResp;
import com.eedi.framework.notice.convert.OrgNoticeReadConvert;
import com.eedi.framework.notice.dal.dataobject.OrgNoticeReadDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Mapper
public interface OrgNoticeReadMapper extends BaseMapperX<OrgNoticeReadDO> {

    default Map<String, String> getRedaStatus(Collection<String> sysNoticeIds, String orgUserId) {
        if (StrUtil.isBlank(orgUserId) || CollUtil.isEmpty(sysNoticeIds)) {
            return Map.of();
        }
        List<OrgNoticeReadDO> orgNoticeRead = selectList(new LambdaQueryWrapperX<OrgNoticeReadDO>()
                .in(OrgNoticeReadDO::getSysNoticeId, sysNoticeIds)
                .eq(OrgNoticeReadDO::getOrgUserId, orgUserId)
        );
        return orgNoticeRead.stream().collect(Collectors.toMap(OrgNoticeReadDO::getSysNoticeId, OrgNoticeReadDO::getOrgNoticeReadId));
    }

    default void create(String orgUserId, String sysNoticeId) {
        if (StrUtil.isBlank(sysNoticeId)) {
            return;
        }
        OrgNoticeReadDO readDO = new OrgNoticeReadDO();
        readDO.setOrgUserId(orgUserId);
        readDO.setSysNoticeId(sysNoticeId);
        insert(readDO);
    }

    default void create(Collection<OrgNoticeReadReq> reqs) {
        if (CollUtil.isEmpty(reqs)) {
            return;
        }
        List<OrgNoticeReadDO> list = reqs.stream().map(OrgNoticeReadConvert.INSTANCE::convert).toList();

        insertBatch(list);
    }

    default OrgNoticeReadResp select(String sysNoticeId, String orgUserId) {
        OrgNoticeReadDO readDO = selectOne(new LambdaQueryWrapperX<OrgNoticeReadDO>()
                .eq(OrgNoticeReadDO::getSysNoticeId, sysNoticeId)
                .eq(OrgNoticeReadDO::getOrgUserId, orgUserId)
        );
        if (ObjectUtil.isEmpty(readDO)) {
            return null;
        }
        return OrgNoticeReadConvert.INSTANCE.convert(readDO);
    }

    default List<OrgNoticeReadResp> selectReadByOrgUserId(String orgUserId) {
        List<OrgNoticeReadDO> orgNoticeReads = selectList(OrgNoticeReadDO::getOrgUserId, orgUserId);
        return orgNoticeReads.stream().map(OrgNoticeReadConvert.INSTANCE::convert).toList();
    }
}
